
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[PlayerClubCurrents_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[PlayerClubCurrents_Insert]

IF OBJECT_ID(N'[dbo].[PlayerClubCurrents_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[PlayerClubCurrents_Update]

IF OBJECT_ID(N'[dbo].[PlayerClubCurrents_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[PlayerClubCurrents_Delete]

IF OBJECT_ID(N'[dbo].[PlayerClubCurrents_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[PlayerClubCurrents_Select]

--endregion

GO


--region [dbo].[PlayerClubCurrents_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[PlayerClubCurrents_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdPlayerClubCurrent int = null,
	@IdPlayer int = null,
	@IdClub int = null,
	@Position nvarchar(100) = null,
	@NumberClothe int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[PlayerClubCurrents]
					WHERE
					(
						(@IdPlayerClubCurrent is null OR [PlayerClubCurrents].[IdPlayerClubCurrent] = @IdPlayerClubCurrent)
						AND (@IdPlayer is null OR [PlayerClubCurrents].[IdPlayer] = @IdPlayer)
						AND (@IdClub is null OR [PlayerClubCurrents].[IdClub] = @IdClub)
						AND (@Position is null OR [PlayerClubCurrents].[Position] like @Position)
						AND (@NumberClothe is null OR [PlayerClubCurrents].[NumberClothe] = @NumberClothe)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[PlayerClubCurrents].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdPlayerClubCurrent' and @SortType = 1 THEN [PlayerClubCurrents].[IdPlayerClubCurrent] END ASC,
						CASE WHEN @SortBy = 'IdPlayerClubCurrent' and @SortType = 0 THEN [PlayerClubCurrents].[IdPlayerClubCurrent] END DESC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 1 THEN [PlayerClubCurrents].[IdPlayer] END ASC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 0 THEN [PlayerClubCurrents].[IdPlayer] END DESC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 1 THEN [PlayerClubCurrents].[IdClub] END ASC,
						CASE WHEN @SortBy = 'IdClub' and @SortType = 0 THEN [PlayerClubCurrents].[IdClub] END DESC,
						CASE WHEN @SortBy = 'Position' and @SortType = 1 THEN [PlayerClubCurrents].[Position] END ASC,
						CASE WHEN @SortBy = 'Position' and @SortType = 0 THEN [PlayerClubCurrents].[Position] END DESC,
						CASE WHEN @SortBy = 'NumberClothe' and @SortType = 1 THEN [PlayerClubCurrents].[NumberClothe] END ASC,
						CASE WHEN @SortBy = 'NumberClothe' and @SortType = 0 THEN [PlayerClubCurrents].[NumberClothe] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[PlayerClubCurrents]
			Where 
			(
				(@IdPlayerClubCurrent is null OR [PlayerClubCurrents].[IdPlayerClubCurrent] = @IdPlayerClubCurrent)
				AND (@IdPlayer is null OR [PlayerClubCurrents].[IdPlayer] = @IdPlayer)
				AND (@IdClub is null OR [PlayerClubCurrents].[IdClub] = @IdClub)
				AND (@Position is null OR [PlayerClubCurrents].[Position] like @Position)
				AND (@NumberClothe is null OR [PlayerClubCurrents].[NumberClothe] = @NumberClothe)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[PlayerClubCurrents_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[PlayerClubCurrents_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[PlayerClubCurrents_Insert]
	@IdPlayerClubCurrent int OUTPUT,
	@IdPlayer int,
	@IdClub int,
	@Position nvarchar(100),
	@NumberClothe int

AS


INSERT INTO [dbo].[PlayerClubCurrents] 
(
	[IdPlayer],
	[IdClub],
	[Position],
	[NumberClothe]
)
VALUES 
(
	@IdPlayer,
	@IdClub,
	@Position,
	@NumberClothe
)

SET @IdPlayerClubCurrent = SCOPE_IDENTITY()

--end [dbo].[PlayerClubCurrents_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[PlayerClubCurrents_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[PlayerClubCurrents_Update]
	@IdPlayerClubCurrent int,
	@IdPlayer int,
	@IdClub int,
	@Position nvarchar(100),
	@NumberClothe int
AS


UPDATE [dbo].[PlayerClubCurrents] SET
	[IdPlayer] = @IdPlayer,
	[IdClub] = @IdClub,
	[Position] = @Position,
	[NumberClothe] = @NumberClothe
WHERE
	[IdPlayerClubCurrent] = @IdPlayerClubCurrent

--end [dbo].[PlayerClubCurrents_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[PlayerClubCurrents_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[PlayerClubCurrents_Delete]
	@IdPlayerClubCurrent int
AS


DELETE FROM [dbo].[PlayerClubCurrents]
WHERE
(
	[IdPlayerClubCurrent] = @IdPlayerClubCurrent
)

--end [dbo].[PlayerClubCurrents_Delete]
--endregion

GO
--=========================================================================================--

